package com.example.administrator.myapplication;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by Administrator on 2020/11/12.
 */

public class CommonDialog extends AlertDialog{

    private TextView titleTv;
    private TextView contentTv;
    private Button leftBtn;
    private Button rightBtn;
    private String title;
    private String content;
    private String positive;
    private String negtive;


    protected CommonDialog(@NonNull Context context) {
        super(context);

    }
    //自定义对话框加载对应的视图
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_dialog);
        initView();
        ivitEvent();
    }
    private void initView(){
        titleTv = findViewById(R.id.custom_title_tv);
        contentTv = findViewById(R.id.custom_content_tv);
        leftBtn = findViewById(R.id.custom_left_bt);
        rightBtn = findViewById(R.id.custom_right_bt);
    }
    private void refreshView(){
        //如果定义title和content的内容，则会弹出框显示
        if(!TextUtils.isEmpty(title)){
               titleTv.setText(title);
               titleTv.setVisibility(View.VISIBLE);//显示
    }else {
        titleTv.setVisibility(View.GONE);//隐藏
           }
           if(!TextUtils.isEmpty(content)){
               contentTv.setText(content);
               contentTv.setVisibility(View.VISIBLE);
           }else {
               contentTv.setVisibility(View.GONE);
           }
        if(!TextUtils.isEmpty(positive)){
               rightBtn.setText(positive);
           }else {
               rightBtn.setText("确定");
           }
        if(!TextUtils.isEmpty(negtive)){
                 leftBtn.setText(negtive);
           }else {
               leftBtn.setText("取消");
           }
    }
    private void ivitEvent(){
           leftBtn.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                   if(onClickBottomListener !=null){
                       onClickBottomListener.onLeftBottomClick();
                   }
               }
           });
           rightBtn.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                  if(onClickBottomListener!=null){
                      onClickBottomListener.onRightBottomClick();
                  }
                   }
           });
    }

    @Override
    public void show() {
        super.show();
        refreshView();
    }

    //第一步提供接口
    public interface OnClickBottomListener{
         void onLeftBottomClick();
         void onRightBottomClick();
    }
    //设置“确定”“取消”按钮的回调
    public OnClickBottomListener onClickBottomListener;
    //第二步提供设置监听事件的方法，并提供回调方法
    public CommonDialog setOnClickBottomListener(OnClickBottomListener onClickBottomListener){
        this.onClickBottomListener=onClickBottomListener;
        return this;
    }
//    public CommonDialog setPositiv(String positive){
//        this.positive=positive;
//        return this;
//    }
//    public CommonDialog setNegtive(){
//        this.negtive=negtive
//        return this;
//    }

    public CommonDialog setTitle(String title){
        this.title=title;
        return this;
    }
    public CommonDialog setContent(String content){
        this.content=content;
        return this;
    }
    public CommonDialog setPositive(String positive){
        this.positive=positive;
        return this;
    }
    public CommonDialog setNegtive(String negtive){
        this.negtive=negtive;
        return this;
    }
}

